# coding=utf8


def iter_readable(iter_obj):
    return '[%s]' % ',\n'.join(map(str, iter_obj))


PRIMITIVE_TYPE = {
    "void": "V",
    "boolean": "Z",
    "byte": "B",
    "short": "S",
    "int": "I",
    "long": "J",
    "char": "C",
    "float": "F",
    "double": "D",
}


class ClassNameUtil:

    @staticmethod
    def get_array_class_name(class_name: str) -> str:
        return '[' + ClassNameUtil.to_descriptor(class_name)

    @staticmethod
    def to_descriptor(class_name: str):
        if class_name.startswith('['):
            return class_name
        if class_name in PRIMITIVE_TYPE:
            return PRIMITIVE_TYPE[class_name]
        return 'L' + class_name + ';'

    @staticmethod
    def to_class_name(descriptor: str) -> str:
        if descriptor.startswith('['):
            return descriptor
        if descriptor.startswith('L'):
            return descriptor[1: -1]
        for class_name, _descriptor in PRIMITIVE_TYPE.items():
            if _descriptor == descriptor:
                return class_name

    @staticmethod
    def get_component_class_name(array_class_name: str) -> str:
        if array_class_name.startswith('['):
            descriptor = array_class_name[1:]
            return ClassNameUtil.to_class_name(descriptor)


INT_DEFAULT_VALUE_DESCRIPTOR_SET = set('BIJSZC')
INT_DEFAULT_VALUE_DESCRIPTOR_SET.update({'boolean', 'byte', 'short', 'int', 'long', 'char'})
FLOAT_DEFAULT_VALUE_DESCRIPTOR_SET = set('DF')
INT_DEFAULT_VALUE_DESCRIPTOR_SET.update({'double', 'float'})


def java_default_of(descriptor):
    value = None
    if descriptor in INT_DEFAULT_VALUE_DESCRIPTOR_SET:
        value = 0
    elif descriptor in FLOAT_DEFAULT_VALUE_DESCRIPTOR_SET:
        value = 0.0
    return value


__all__ = ['java_default_of', 'INT_DEFAULT_VALUE_DESCRIPTOR_SET',
           'FLOAT_DEFAULT_VALUE_DESCRIPTOR_SET',
           'PRIMITIVE_TYPE', 'iter_readable', 'ClassNameUtil']
